import socket
import threading
import time
import random
'''
net.WS_Server(threading.Threading)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WebSocket服务端监听程序
'''
class Server(threading.Thread):  
    
    def __init__(self,address,listen=100):  
        '''
        Feature
            __init__(self,address,listen)
        Description
            初始化
        Args
            address
                type:ip address ("127.0.0.1",3000)
                description:IP地址与端口
            listen
                type:int
                default:100
                description:监听数量
        '''
        threading.Thread.__init__(self) #初始化父类Thread  
        self.socket = None              #socket
        self.s_address=address          #address
        self.s_listen=listen            #listen number
        self.connectNum=0               #connect number
        self.classname=None             #request process
        self.namespace=None             #request namespace
        self.method=None                #request process runtime method

    def configRequest(self,_namespace,_classname,_method):
        '''
        Feature
            configRequest(self,_namespace,_classname,_method)
        Description
            配置client处理线程
        Args
            _namespace
                type:string
                description:命名空间
            _classname
                type:string
                description:类名
            _method
                type:string
                description:处理方法
        '''
        self.namespace=_namespace
        self.classname=_classname
        self.method=_method

    def run(self):
        '''
        Feature
            run(self)
        Description
            处理线程
        '''  
        print('WebSocketServer Start!')  
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
        self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)  
        self.socket.bind(self.s_address)  
        self.socket.listen(self.s_listen)  
        while True:  
            connection, address = self.socket.accept() 
            if connection:
                handle=__import__(self.namespace+"."+self.classname,fromlist=(True))
                if hasattr(handle,self.classname):
                    func=getattr(handle,self.classname)
                    if func:
                        newSocket=func(connection,address,self)
                        if newSocket:
                            if hasattr(newSocket,"run"):    #检查是否有run方法
                                run=getattr(newSocket,self.method)
                                if run:
                                    run()
                                    self.connectNum = self.connectNum + 1
                            else:
                                print("Error:Don't found method run!")
                        else:
                            print("Error:Create object {0}.{1} Fail!".format(self.namespace,self.classname))
                else:
                    print("Error:Don't found class {0}!".format(self.classname))